// import '@/utils/css/ol.css';
import 'ol/ol.css'
import {
  Map,
  View
} from 'ol'
import TileLayer from 'ol/layer/Tile'
import ImageLayer from 'ol/layer/Image'
import Static from 'ol/source/ImageStatic'
import Projection from 'ol/proj/Projection'
import {
  baseUrl
} from '@/utils/global'
import OSM from 'ol/source/OSM'
import {
  defaults as defaultControls
} from 'ol/control'
import {
  createStringXY
} from 'ol/coordinate'
import MousePosition from 'ol/control/MousePosition'
import {
  getCenter
} from 'ol/extent'
import {
  Draw,
  Modify,
  Snap,
  Select,
  Translate,
  Interaction,
  defaults as defaultInteractions,
  DragRotateAndZoom
} from 'ol/interaction.js'
import {
  Style,
  Icon,
  Text,
  Fill,
  Stroke,
  Circle
} from 'ol/style'
import Overlay from 'ol/Overlay'
import {
  Vector as VectorLayer,
  Image
} from 'ol/layer.js'
import {
  Vector
} from 'ol/source.js'
import {
  GeoJSON
} from 'ol/format.js'

import Cookie from 'js-cookie'

import {
  getLayerFenceDataList,
  getSingeFence,
  loadType,
  getAllFence
} from '@/api/system/historyDisplayInfo.js'
import {
  getAllFenceLayer,
  allFenceLyaer
} from '@/api/cy.js'
import Point from 'ol/geom/Point'
import Feature from 'ol/Feature'

let _this = null // 用来存储Vue实例this
let _iMap = null
let _iMap2 = null
let _dPR
let _dPR2
let _zero
let _Style
let OverlayLayer
var TooltipElement
let Tooltip
let OverlayPopup
let measureTooltipElement
let measureTooltip
let tagTypes
var mapZoom = 2
var FenceSource = null
var FenceLayer = null

var FenceSource2
var FenceLayer2
// 传递this对象
function sendThis(vm) {
  _this = vm
}

function Load_Map(url, RealWidth, RealHeight, PixelWidth, PixelHeight, dPR, ZeroPoint) {
  _dPR = dPR
  _zero = _TransPixel(ZeroPoint)
  var left = _zero[0]
  var bottom = _zero[1] + (-PixelHeight)
  var right = _zero[0] + PixelWidth
  var top = _zero[1]
  var _Projection = new Projection({
    code: 'EPSG:3857',
    units: 'pixels',
    extent: [left, bottom, right, top]
  })

  _iMap = new Map({
    interactions: defaultInteractions().extend([
      new DragRotateAndZoom()
    ]),
    target: 'map',
    layers: [
      new ImageLayer({
        source: new Static({
          url: baseUrl + url,
          projection: _Projection,
          imageExtent: [left, bottom, right, top]
        })
      })
    ],
    view: new View({
      projection: _Projection,
      center: getCenter([left, bottom, right, top]),
      zoom: 2,
      maxZoom: 8
    })
  })

  _iMap.on('pointermove', MapPointerMove) // 监听鼠标移动事件
  _iMap.getView().on('change:resolution', calcScale) // 监听地图缩放事件
  calcScale()
  _iMap.on('click', MapClick) // 监听点击事件
}

function Load_TrackMap(url, RealWidth, RealHeight, PixelWidth, PixelHeight, dPR, ZeroPoint) {
  _dPR2 = dPR
  const _zero = _TransPixel(ZeroPoint)
  var left = _zero[0]
  var bottom = _zero[1] + (-PixelHeight)
  var right = _zero[0] + PixelWidth
  var top = _zero[1]
  var _Projection = new Projection({
    code: 'EPSG:3857',
    units: 'pixels',
    extent: [left, bottom, right, top]
  })

  _iMap2 = new Map({
    interactions: defaultInteractions().extend([
      new DragRotateAndZoom()
    ]),
    target: 'map2',
    layers: [
      new ImageLayer({
        source: new Static({
          url: url,
          projection: _Projection,
          imageExtent: [left, bottom, right, top]
        })
      })
    ],
    view: new View({
      projection: _Projection,
      center: getCenter([left, bottom, right, top]),
      zoom: 2,
      maxZoom: 8
    })
  })
}

/**
 * 加载鼠标工具tooltip
 */
function _createTooltip(t) {
  if (TooltipElement) {
    TooltipElement.parentNode.removeChild(TooltipElement)
  }
  TooltipElement = document.createElement('div')
  TooltipElement.className = 'maptooltip hidden'
  Tooltip = new Overlay({
    element: TooltipElement,
    offset: [10, 0],
    positioning: 'center-left'
  })
}

// 坐标函数
function MapPointerMove(evt) {
  if (evt.dragging) {
    return
  }
  var coord = _TransMetric_XY(evt.coordinate, 2)
  $('.positiondiv span').html(coord)
}

// 输出坐标点位置
function _TransMetric_XY(c, fix) {
  var X = (c[0] / _dPR).toFixed(fix)
  var Y = (c[1] / _dPR).toFixed(fix) * -1
  var M = [X, Y]
  var M = 'X:' + X + '  ' + 'Y:' + Y
  return M
}
// 坐标（锚点、标签） 转换成像素(米-》像素)
function _TransPixel(c) {
  var PX
  $.each(c, function(i, item) {
    if (item == undefined || item.length == undefined) {
      var X = c[0] * _dPR
      var Y = _validate(c[1]) * _dPR
      PX = [X, Y]
    } else {
      $.each(c, function(i, item) {
        var j, jj
        for (j = 0, jj = item.length; j < jj; ++j) {
          item[j] = [item[j][0] * _dPR, _validate(item[j][1]) * _dPR]
        }
        PX = [item]
      })
    }
  })
  return PX
}

// 坐标（锚点、标签）  转换成米（像素-》米）
function _TransMetric(c) {
  var M = []
  c.forEach(function(item) {
    if (item.length == undefined) {
      var X = (c[0] / _dPR).toFixed(2)
      var Y = _validate((c[1] / _dPR)).toFixed(2)
      M = [X, Y]
    } else {
      M.push('[' + [(item[0] / _dPR).toFixed(2), (_validate(item[1]) / _dPR).toFixed(2)] + ']')
    }
  })
  return M
}

function _TransMetric2(c, fix) {
  var M
  $.each(c, function(i, item) {
    if (item.length == undefined) {
      var X = (c[0] / _dPR).toFixed(fix)
      var Y = _validate((c[1] / _dPR)).toFixed(fix)
      M = [X, Y]
    } else {
      $.each(c, function(i, item) {
        var j, jj
        for (j = 0, jj = item.length; j < jj; ++j) {
          item[j] = '[' + [(item[j][0] / _dPR).toFixed(fix), (_validate(item[j][1]) / _dPR).toFixed(fix)] + ']'
        }
        M = [item]
      })
    }
  })
  return M
}

function _TransMetric3(c, fix) {
  var M
  $.each(c, function(i, item) {
    if (item.length == undefined) {
      var X = (c[0] / _dPR).toFixed(fix)
      var Y = _validate((c[1] / _dPR)).toFixed(fix)
      M = [X, Y]
    } else {
      $.each(c, function(i, item) {
        var j, jj
        for (j = 0, jj = item.length; j < jj; ++j) {
          item[j] = [(item[j][0] / _dPR).toFixed(fix), (_validate(item[j][1]) / _dPR).toFixed(fix)]
        }
        M = item
      })
    }
  })
  return M
}

function createMeasureTooltip() {
  if (measureTooltipElement) {
    measureTooltipElement.parentNode.removeChild(measureTooltipElement)
  }
  measureTooltipElement = document.createElement('div')
  measureTooltipElement.className = 'ol-tooltip ol-tooltip-measure'
  measureTooltip = new Overlay({
    element: measureTooltipElement,
    offset: [0, -15],
    positioning: 'bottom-center'
  })
  _iMap.addOverlay(measureTooltip)
}
// 坐标（电子围栏页面显示标尺）像素-》米
function _TransMetricFence(c) {
  var M = []
  c.forEach(function(item) {
    if (item.length == undefined) {
      var X = (c[0] / _dPR).toFixed(2)
      var Y = (c[1] / _dPR)
      M = [X, Y]
    } else {
      M.push([(item[0] / _dPR).toFixed(2), (-(item[1] / _dPR)).toFixed(2)])
    }
  })
  return '长:' + (parseFloat(M[1][0]) - parseFloat(M[0][0])).toFixed(2) + '宽' + (parseFloat(M[1][1]) - parseFloat(M[2][1])).toFixed(2)
}
// 监听地图缩放事件
function calcScale() {
  mapZoom = _iMap.getView().getZoom()
  const res = _iMap.getView().getResolution()
  const temp = (100 / _dPR) * res
  $('.scalediv span').html(temp.toFixed(2) + 'M')
}
// 取数字的绝对值
function _validate(num) {
  var reg = /^\d+(?=\.{0,1}\d+$|$)/
  if (reg.test(num)) return -(num)
  return Math.abs(num)
}
// 获取目前所有数据类型
function LoadTypeData() {
  tagTypes = []
  loadType().then(data => {
    if (data.code === 200) {
      // 按大类进行分类整理
      const datas = data.data
      for (let i = 0; i < datas.length; i++) {
        const type = datas[i].type
        if (tagTypes[type]) {
          tagTypes[type].push(datas[i])
        } else {
          tagTypes[type] = []
          tagTypes[type].push(datas[i])
        }
      }
    }
  })
}
LoadTypeData()

// 标签样式设置
function TagStyle(f, rontation) {
  let textname = ''
  let iconsrc = ''
  let iconcolor = ''
  textname = mapZoom >= 2 ? f.get('name') : ''
  // 判断标签的类型及工种，用来显示不同的图标和颜色
  if (tagTypes[f.get('type')]) {
    const subtypes = tagTypes[f.get('type')]
    var b = -1
    for (let i = 0; i < subtypes.length; i++) {
      if (subtypes[i].name === f.get('workTypeName')) {
        b = i
        break
      }
    }
    if (b != -1) { // 有这种类型的工种
      iconsrc = '/static/icon/' + subtypes[b].icon + '.png'
      iconcolor = subtypes[b].color
    } else {
      iconsrc = '/static/icon/5ren.png'
      iconcolor = '#409EFF'
    }
  } else {
    // 如果没有这种大类，使用默认图标
    iconsrc = '/static/icon/5ren.png'
    iconcolor = '#409EFF'
  }
  var zIndex = 999
  if (!f.get('visible')) {
    return null
  }

  _Style = new Style({
    image: new Icon({
      src: iconsrc,
      color: iconcolor
    }),
    text: new Text({
      text: textname,
      offsetY: 14,
      fill: new Fill({
        color: '#fff'
      }),
      stroke: new Stroke({
        color: '#000',
        width: 3
      }),
      rotation: rontation
    }),
    zIndex: 999
  })
  return [
    _Style
  ]
}
function _Popup(f, pos) {
  OverlayPopup = _iMap.getOverlayById('popup_')
  const content = $(OverlayPopup.getElement()).find('#popup-content')
  const closer = $(OverlayPopup.getElement()).find('#popup-closer')
  const trackHistory = $(OverlayPopup.getElement()).find('#trackHistory')
  trackHistory.on('click', function() {
    _this.isDialog = true
  })
  closer.click(function() {
    OverlayPopup.setPosition(undefined)
    this.blur()
    return false
  })
  OverlayPopup.setPosition(pos)
  OverlayPopup.set('idx_', f.getId())
  _this.featureId = f.getId()
  if (f) {
    var c
    switch (f.get('type')) {
      case 'staff':
        c = '<li>编号：' + f.get('resourceId') + '</li><li>名称：' + f.get('itemname') + '</li><li>坐标：' + _TransMetric(pos) + '</li><li>数量：' + +'</li><li>状态：' + +'</li>'
        break

      default:
        break
    }
    content.html(c)
  } else {
    OverlayPopup.setPosition(undefined)
  }
}

// 加载气泡
// function _Popup(f, pos) {
//   var element = document.getElementById('popup')
//   $(element).css({
//     'display': 'block'
//   })
//   var content = $('#popup-content')
//   var closer = $('#popup-closer')
//   var Popup = new Overlay({
//     id: f.getId(),
//     element: element,
//     positioning: 'center-center',
//     offset: [0, 0],
//     stopEvent: false,
//     autoPan: true,
//     autoPanAnimation: {
//       duration: 250
//     },
//     position: pos
//   })
//   closer.click(function() {
//     Popup.setPosition(undefined)
//     this.blur()
//     return false
//   })
//   _iMap.addOverlay(Popup)
//   OverlayLayer = _iMap.getOverlayById(f.getId())
//   if (f) {
//     var c
//     switch (f.get('type')) {
//       case 'staff':
//         c = '<li>名字：' + f.get('name') + '</li><li>工号：' + f.get('resourceId') + '</li><li>部门：' + f.get('deptname') + '</li><li>标签ID：' + f.get('tag_id') + '</li>'
//         break

//       default:
//         break
//     }
//     content.html(c)
//   } else {
//     Popup.setPosition(undefined)
//   }
// }

// 更新overlay位置
function updateOverlayPosition(tagid, ncoords) {
  OverlayLayer = _iMap.getOverlayById(tagid)
  if (OverlayLayer != null) {
    if (OverlayLayer.getId() === tagid) {
      if (OverlayLayer.getPosition() != undefined) {
        OverlayLayer.setPosition(ncoords)
      }
    }
  }
}

function FenceStyle(f, r) {
  const orderNum = '--顺序:' + f.get('orderNum')
  const operation = '--工序名:' + f.get('operationName')
  const name = f.get('name')
  var changeName
  if (f.get('orderNum') && f.get('operationName')) {
    changeName = name + orderNum + operation
  } else {
    changeName = name
  }

  return [
    new Style({
      fill: new Fill({
        color: 'rgba(' + r + ',0.1)'
      }),
      stroke: new Stroke({
        lineDash: [1, 2, 3, 4, 5, 6],
        color: 'rgb(' + r + ')',
        width: 2
      }),
      image: new Circle({
        radius: 7,
        fill: new Fill({
          color: '#FF0000'
        })
      }),
      text: new Text({
        text: changeName,
        font: 'bold 14px Arial',
        textAlign: 'center',
        textBaseline: 'middle',
        overflow: true,
        offsetX: 0,
        offsetY: 0,
        fill: new Fill({
          color: '#ff0000'
        }),
        stroke: new Stroke({
          color: '#fff',
          width: 3
        })
      })
    })
  ]
}

function FenceStyleOrder(f, r) {
  const orderNum = '--顺序:' + f.get('orderNum')
  const operation = '--工序名:' + f.get('operationName')

  const name = f.get('name')
  // if (name == undefined || name == null || name == "" || name == "无") {
  //   name = f.get('name')
  // }
  const changeName = name + orderNum + operation
  return [
    new Style({
      fill: new Fill({
        color: 'rgba(' + r + ',0.1)'
      }),
      stroke: new Stroke({
        lineDash: [1, 2, 3, 4, 5, 6],
        color: 'rgb(' + r + ')',
        width: 2
      }),
      image: new Circle({
        radius: 7,
        fill: new Fill({
          color: '#FF0000'
        })
      }),
      text: new Text({
        text: changeName,
        font: 'bold 14px Arial',
        textAlign: 'center',
        textBaseline: 'middle',
        overflow: true,
        offsetX: 10,
        offsetY: 0,
        fill: new Fill({
          color: '#ff0000'
        }),
        stroke: new Stroke({
          color: '#fff',
          width: 3
        })
      })
    })
  ]
}

// 增加电子围栏
function Load_Fence(Geo, type) {
  if (FenceLayer != null) {
    _iMap.removeLayer(FenceLayer)
    FenceSource = null
    FenceLayer = null
  }
  FenceSource = new Vector({
    wrapX: false
  })
  FenceLayer = new VectorLayer({
    minResolution: 0,
    maxResolution: _dPR * 1.2,
    source: FenceSource,
    style: function(feature, resolution) {
      const fenceType = feature.get('fencetype')
      // if (feature.get('orderNum') || feature.get('operationName')) {
      //   return FenceStyleOrder(feature, '0, 255, 0')
      // }

      if (feature.get('id') == undefined) {
        return FenceStyle(feature, '255, 0, 0')
      }

      if (!(typeof fenceType === 'undefined')) {
        const arr = fenceType.split(',')
        if (arr.length > 1) {
          return FenceStyle(feature, '165,42,42')
        }
      }

      if (fenceType == 'camera') {
        return FenceStyle(feature, '0, 153, 102')
      } else if (fenceType == 'operation') {
        return FenceStyle(feature, '0,153,204')
      } else if (fenceType == 'alarm') {
        return FenceStyle(feature, '255,0,0')
      } else if (fenceType == 'check') {
        return FenceStyle(feature, '255,255,0')
      } else if (fenceType == 'polling') {
        return FenceStyle(feature, '255,165,0')
      } else {
        return FenceStyle(feature, '160,32,240')
      }
    }
  })
  _iMap.addLayer(FenceLayer)
  if (Geo != '') {
    if (type === 'all' || type === undefined) {
      FenceAllLayer({ mapId: Geo }, FenceSource)
      // FenceAjax(Geo, FenceSource)
    } else {
      FenceAllLayer({ mapId: Geo, type: type }, FenceSource)
      // FenceAjaxLayer(Geo, type, FenceSource)
    }
  }
}

function FenceAllLayer(params, source) {
  allFenceLyaer(params).then(res => {
    var format = new GeoJSON()
    if (res.code === 200) {
      if (res.data.length > 0) {
        for (let i = 0; i < res.data.length; i++) {
          var newFeatures = format.readFeatures(res.data[i])
          newFeatures.forEach(function(f) {
            if (f.get('fencetype') != 'layer' && f.get('fencetype') != 'temporary') {
              var coord = f.getGeometry().getCoordinates()
              f.getGeometry().setCoordinates(_TransPixel(coord))
              f.setId(f.get('id'))
              source.addFeature(f)
            }
          })
        }
      }
    }
  })
}

/**
 * AJAX获取电子围栏GeoJson数据
 */
// function FenceAjax(Geo, source) {
//   getAllFence(Geo).then(res => {
//     var format = new GeoJSON()
//     if (res.code === 200) {
//       if (res.data.length > 0) {
//         for (let i = 0; i < res.data.length; i++) {
//           var newFeatures = format.readFeatures(res.data[i])
//           newFeatures.forEach(function(f) {
//             //   console.log(f.get('id'), f.getGeometry().getCoordinates(), f.get('name'), f.get('fencetype'), f)
//             if (f.get('fencetype') != 'layer' && f.get('fencetype') != 'temporary') {
//               var coord = f.getGeometry().getCoordinates()
//               f.getGeometry().setCoordinates(_TransPixel(coord))
//               f.setId(f.get('id'))
//               source.addFeature(f)
//             }
//           })
//         }
//       }
//     }
//   })
// }

/**
 * AJAX获取电子围栏GeoJson数据
 */
// function FenceAjaxLayer(Geo, type, source) {
//   getAllFenceLayer(Geo, type).then(res => {
//     var format = new GeoJSON()
//     if (res.code === 200) {
//       if (res.data.length > 0) {
//         for (let i = 0; i < res.data.length; i++) {
//           var newFeatures = format.readFeatures(res.data[i])
//           newFeatures.forEach(function(f) {
//             if (f.get('fencetype') != 'layer' && f.get('fencetype') != 'temporary') {
//               var coord = f.getGeometry().getCoordinates()
//               f.getGeometry().setCoordinates(_TransPixel(coord))
//               f.setId(f.get('id'))
//               source.addFeature(f)
//             }
//           })
//         }
//       }
//     }
//   })
// }

// 增加电子围栏
function Load_Fence2() {
  if (FenceLayer2 != null) {
    FenceSource2.clear()
    _iMap.removeLayer(FenceLayer2)
    FenceSource2 = null
    FenceLayer2 = null
  }
  FenceSource2 = new Vector({
    wrapX: false
  })
  FenceLayer2 = new VectorLayer({
    minResolution: 0,
    maxResolution: _dPR * 1.2,
    source: FenceSource2,
    style: function(feature, resolution) {
      const fenceType = feature.get('fencetype')
      if (feature.get('id') == undefined) {
        return FenceStyle(feature, '255, 0, 0')
      }
      if (!(typeof fenceType === 'undefined')) {
        const arr = fenceType.split(',')
        if (arr.length > 1) {
          return FenceStyle(feature, '165,42,42')
        }
      }

      if (fenceType == 'camera') {
        return FenceStyle(feature, '0, 153, 102')
      } else if (fenceType == 'operation') {
        return FenceStyle(feature, '0,153,204')
      } else if (fenceType == 'alarm') {
        return FenceStyle(feature, '255,0,0')
      } else if (fenceType == 'check') {
        return FenceStyle(feature, '255,255,0')
      } else if (fenceType == 'polling') {
        return FenceStyle(feature, '255,165,0')
      } else {
        return FenceStyle(feature, '160,32,240')
      }
    }
  })
  _iMap.addLayer(FenceLayer2)
}

// 释放围栏容器
function releaseFence() {
  if (FenceSource != null) {
    const old = FenceSource.getFeatures()
    let len = old.length
    while (len > 0) {
      len--
      FenceSource.removeFeature(old[len])
      old.splice(len, 1)
    }
  }
  if (FenceLayer != null) {
    _iMap.removeLayer(FenceLayer)
  }

  FenceSource = null
  FenceLayer = null
}

// 清除地图中的围栏-cy
function clearFence() {
  if (FenceSource != null) {
    const old = FenceSource.getFeatures() // 现在已有的标签数据
    let len = old.length
    while (len > 0) {
      len--
      FenceSource.removeFeature(old[len])
      old.splice(len, 1)
    }
  }
}

// 获取围栏资源列表
function getFenceList(mapId) {
  getLayerFenceDataList(mapId).then(res => {
    if (res.code == 200) {
      _this.tableFence = res.data
      _this.tableFence.forEach((val) => {
        val.layerName = null
        val.creator = null
      })
    }
  })
}

// 通过围栏id加载单个围栏或多个
function addSingleFence(param) {
  if (FenceLayer != null) {
    _iMap.removeLayer(FenceLayer)
    FenceSource = null
    FenceLayer = null
  }
  FenceSource = new Vector({
    wrapX: false
  })
  FenceLayer = new VectorLayer({
    minResolution: 0,
    maxResolution: _dPR * 1.2,
    source: FenceSource,
    style: function(feature, resolution) {
      const fenceType = feature.get('fencetype')
      if (feature.get('id') == undefined) {
        return FenceStyle(feature, '255, 0, 0')
      }
      if (!(typeof fenceType === 'undefined')) {
        const arr = fenceType.split(',')
        if (arr.length > 1) {
          return FenceStyle(feature, '165,42,42')
        }
      }

      if (fenceType == 'camera') {
        return FenceStyle(feature, '0, 153, 102')
      } else if (fenceType == 'operation') {
        return FenceStyle(feature, '0,153,204')
      } else if (fenceType == 'alarm') {
        return FenceStyle(feature, '255,0,0')
      } else if (fenceType == 'check') {
        return FenceStyle(feature, '255,255,0')
      } else if (fenceType == 'polling') {
        return FenceStyle(feature, '255,165,0')
      } else {
        return FenceStyle(feature, '160,32,240')
      }
    }
  })
  _iMap.addLayer(FenceLayer)
  if (typeof param === 'number') {
    getSingeFence(param).then(res => {
      var format = new GeoJSON()
      var newFeatures = format.readFeatures(res.data[0])
      newFeatures.forEach(function(f) {
        if (f.get('fencetype') != 'layer' && f.get('fencetype') != 'temporary') {
          var coord = f.getGeometry().getCoordinates()
          f.getGeometry().setCoordinates(_TransPixel(coord))
          f.setId(f.get('id'))
          FenceSource.addFeature(f)
        }
      })
    })
  } else {
    param.forEach((val) => {
      getSingeFence(val.idx).then(res => {
        var format = new GeoJSON()
        var newFeatures = format.readFeatures(res.data[0])
        newFeatures.forEach(function(f) {
          if (f.get('fencetype') != 'layer' && f.get('fencetype') != 'temporary') {
            var coord = f.getGeometry().getCoordinates()
            f.getGeometry().setCoordinates(_TransPixel(coord))
            f.setId(f.get('id'))
            FenceSource.addFeature(f)
          }
        })
      })
    })
  }
}

// 根据围栏id修改单个电子围栏的样式信息
function getFenceById(fenceId, color1, color2, fenceType, fenceName) {
  if (FenceSource == null) {
    _this.$message({
      message: '围栏未被勾选显示'
    })
  }

  if (color1 == null) {
    if (fenceType == 'camera') {
      color1 = 'rgba(0, 153, 102,0.1)'
    } else if (fenceType == 'operation') {
      color1 = 'rgba(0,153,204,0.1)'
    } else if (fenceType == 'alarm') {
      color1 = 'rgba(255,0,0,0.1)'
    } else if (fenceType == 'check') {
      color1 = 'rgba(255,255,0,0.1)'
    } else if (fenceType == 'polling') {
      color1 = 'rgba(255,165,0,0.1)'
    } else {
      color1 = 'rgba(160,32,240,0.1)'
    }
  }

  if (color2 == null) {
    if (fenceType == 'camera') {
      color2 = 'rgba(0, 153, 102,1)'
    } else if (fenceType == 'operation') {
      color2 = 'rgba(0,153,204,1)'
    } else if (fenceType == 'alarm') {
      color2 = 'rgba(255,0,0,1)'
    } else if (fenceType == 'check') {
      color2 = 'rgba(255,255,0,1)'
    } else if (fenceType == 'polling') {
      color2 = 'rgba(255,165,0,1)'
    } else {
      color2 = 'rgba(160,32,240,1)'
    }
  }

  var feature = FenceSource.getFeatureById(fenceId)
  feature.setStyle(new Style({
    fill: new Fill({
      color: color1
    }),
    stroke: new Stroke({
      lineDash: [1, 2, 3, 4, 5, 6],
      color: color2,
      width: 2
    }),
    image: new Circle({
      radius: 7,
      fill: new Fill({
        color: '#FF0000'
      })
    }),
    text: new Text({
      text: fenceName,
      font: 'bold 14px Arial',
      textAlign: 'center',
      textBaseline: 'middle',
      offsetX: 0,
      offsetY: 0,
      fill: new Fill({
        color: '#FF0000'
      }),
      stroke: new Stroke({
        color: '#fff',
        width: 3
      })
    })
  }))
}

// 地图点击事件
function MapClick(evt) {
  // 识别点击的位置有没有"对象"
  var feature = _iMap.forEachFeatureAtPixel(evt.pixel, function(feature, layer) {
    return feature
  })
  if (feature) {
    const type = feature.get('type')
    if (type === 'staff' || type === 'car' || type === 'material' || type === 'signange' || type === 'workingHoursOrder' || type === 'bucket') {
      // 调用弹出层
      // _Popup(feature, evt.coordinate)
      _Popup(feature, feature.getGeometry().getCoordinates())
    }
  }
}

// 更新overlay位置
function updateOverPopupPosition(f, ncoords) {
  const OverlayPopup = _iMap.getOverlayById('popup_')
  if (OverlayPopup.getPosition()) {
    if (f.getId() == OverlayPopup.get('idx_')) {
      const content = $(OverlayPopup.getElement()).find('#popup-content')
      if (f) {
        var c
        switch (f.get('type')) {
          case 'staff':
            c = '<li>编号：' + f.get('resourceId') + '</li><li>名称：' + f.get('itemname') + '</li><li>坐标：' + _TransMetric(ncoords) + '</li><li>数量：' + +'</li><li>状态：' + +'</li>'
            break
          default:
            break
        }
        content.html(c)
      } else {
        OverlayPopup.setPosition(undefined)
      }
      OverlayPopup.setPosition(ncoords)
    }
  }
}
export {
  Tooltip,
  _createTooltip,
  sendThis,
  Load_Map,
  _TransPixel,
  TagStyle,
  updateOverlayPosition,
  FenceStyle,
  _TransMetric,
  _TransMetric2,
  _TransMetric3,
  _TransMetricFence,
  createMeasureTooltip,
  Load_Fence,
  Load_Fence2,
  releaseFence,
  clearFence,
  getFenceList,
  addSingleFence,
  getFenceById,
  Load_TrackMap,
  updateOverPopupPosition,
  FenceSource,
  FenceSource2,
  _iMap,
  _Style,
  _dPR,
  tagTypes
}
